/**
 * 检查的卢属性计算脚本
 */

const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: '43.143.253.188',
  user: 'nextjs',
  password: 'yEAiRE2jcxTyRBy5',
  database: 'nextjs',
  charset: 'utf8mb4'
};

// 的卢属性配置（根据表格数据）
const diluConfig = {
  name: "的卢",
  attributes: {
    hp: {
      initial: 17100,
      maxLevels: {
        notPromoted: 17100,
        firstPromotion: 25180,
        secondPromotion: 35380,
        thirdPromotion: 47940,
      },
    },
  },
};

// 属性计算公式
function calculateMountAttribute (initialValue, maxValue, maxLevel, currentLevel) {
  const levelRatio = Math.floor(currentLevel) / maxLevel;
  return Math.floor(initialValue + (maxValue - initialValue) * levelRatio);
}

async function checkDiluAttributes () {
  let connection;

  try {
    console.log('🔌 连接数据库...');
    connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功');

    console.log('🐎 检查的卢属性计算...\n');

    // 获取的卢基础数据
    const [baseRows] = await connection.execute(
      'SELECT * FROM mount_base WHERE name = "的卢"'
    );

    if (baseRows.length === 0) {
      console.log('❌ 未找到的卢基础数据');
      return;
    }

    const baseData = baseRows[0];
    console.log('📊 的卢基础数据:');
    console.log(`名称: ${baseData.name}`);
    console.log(`初始气血: ${baseData.hpIni}`);
    console.log(`最大气血: ${baseData.hp}`);

    // 获取角色坐骑中的的卢数据
    const [mountRows] = await connection.execute(`
            SELECT cm.*, mb.name as mountName 
            FROM character_mounts cm 
            JOIN mount_base mb ON cm.mountBaseId = mb.id 
            WHERE cm.characterId = 2001 AND mb.name = "的卢"
        `);

    if (mountRows.length === 0) {
      console.log('❌ 未找到角色的卢数据');
      return;
    }

    const mountData = mountRows[0];
    console.log('\n📊 角色的卢数据:');
    console.log(`名称: ${mountData.mountName}`);
    console.log(`等级: ${mountData.level}`);
    console.log(`转职等级: ${mountData.transLevel}`);
    console.log(`当前气血: ${mountData.currentHp}`);

    // 验证属性计算
    console.log('\n🔍 验证属性计算:');

    const level = mountData.level;
    const transLevel = mountData.transLevel || 0;

    // 根据转职等级确定最大等级和属性上限
    let maxLevel, maxHp;

    switch (transLevel) {
      case 0: // 未转
        maxLevel = 100;
        maxHp = diluConfig.attributes.hp.maxLevels.notPromoted;
        break;
      case 1: // 一转
        maxLevel = 120;
        maxHp = diluConfig.attributes.hp.maxLevels.firstPromotion;
        break;
      case 2: // 二转
        maxLevel = 140;
        maxHp = diluConfig.attributes.hp.maxLevels.secondPromotion;
        break;
      case 3: // 三转
        maxLevel = 160;
        maxHp = diluConfig.attributes.hp.maxLevels.thirdPromotion;
        break;
      default:
        maxLevel = 100;
        maxHp = diluConfig.attributes.hp.maxLevels.notPromoted;
    }

    // 计算期望属性
    const expectedHp = calculateMountAttribute(
      diluConfig.attributes.hp.initial,
      maxHp,
      maxLevel,
      level
    );

    console.log(`转职等级: ${transLevel} (最大等级: ${maxLevel})`);
    console.log(`等级: ${level}`);
    console.log(`期望气血: ${expectedHp}, 实际气血: ${mountData.currentHp}`);

    const hpMatch = expectedHp === mountData.currentHp;
    console.log(`气血匹配: ${hpMatch ? '✅' : '❌'}`);

    if (!hpMatch) {
      console.log('\n❌ 属性计算不正确，需要修复');
      console.log(`差异: ${expectedHp - mountData.currentHp}`);
    } else {
      console.log('\n✅ 属性计算正确');
    }

    // 测试不同等级的属性计算
    console.log('\n📊 测试不同等级的属性计算:');
    console.log('等级\t期望气血\t实际气血\t匹配');
    console.log('----\t--------\t--------\t----');

    for (let testLevel = 1; testLevel <= 100; testLevel += 20) {
      const testHp = calculateMountAttribute(
        diluConfig.attributes.hp.initial,
        maxHp,
        maxLevel,
        testLevel
      );

      const match = testHp === mountData.currentHp;
      console.log(`${testLevel}\t${testHp}\t\t${mountData.currentHp}\t\t${match ? '✅' : '❌'}`);
    }

  } catch (error) {
    console.error('❌ 检查失败:', error.message);
    throw error;
  } finally {
    if (connection) {
      await connection.end();
      console.log('🔌 数据库连接已关闭');
    }
  }
}

// 运行脚本
if (require.main === module) {
  checkDiluAttributes().catch((error) => {
    console.error('💥 脚本执行失败:', error.message);
    process.exit(1);
  });
}

module.exports = { checkDiluAttributes };
